
$(function(){
	$("select").each(function(){
		$(this).append("<option value=''>---请选择---</option>");
	});

	var basePath = $("#basePath").val();
	$.ajax({
		url: basePath + "base_info/ajax_un_pigeonhole_base_data",
		type: "GET",
		dataType: "json",
		success: function( datas ) {
			console.log(datas);
			init(datas);
			
		}
	});
	$("input[name='onlyId']").focus();
	$("input[name='onlyId']").keydown(function(e){
		if(e.keyCode == 13){
			var onlyId = $(this).val();
			if(onlyId.length > 2){
				queryBtnClick();
				$(this).val(null);
			}
			
		}
	});
});

function init(baseInfo){
	var medicalDepts = baseInfo['medicalDepts'];
	$("select[name='outHospitalDeptCode'],[name='outDeptCode']").each(function(){
		var options = "";
		for(var index in medicalDepts){
			var medicalDept = medicalDepts[index];
	    	options+="<option value='"+medicalDept.uniq_code+"'>"+medicalDept.label+"</option>"
	    } 
       $(this).append(options);
       
	});
	
	var outHospitalTypes = baseInfo['outHospitalTypes'];
	$("select[name='outHospitalTypeCode']").each(function(){
		var options = "";
		for(var index in outHospitalTypes){
			var outHospitalType = outHospitalTypes[index];
	    	options+="<option value='"+outHospitalType.uniq_code+"'>"+outHospitalType.label+"</option>"
	    } 
       $(this).append(options);
	});
}

var addFormIndex;
var pigeonholeFormIndex;

/*点击查询按钮*/
function queryBtnClick(){
	$("input[name='currentPage']").val(1);
	queryFormSubmit();
}

/*提交查询*/
function queryFormSubmit(){
	var submitData = $('#queryForm').serialize();
	var basePath = $("#basePath").val();
	layer.load(1);
	
	clearPage();
	$.ajax({
		url: basePath + "interface_medical_record1/ajax_query_interface_medical_record_dr_pt",
		dataType: "json",
		data:submitData,
		success: function( data ) {
			console.log(data);
			var success = data['success'];
			if(success){
				var stateCode =data['stateCode']
				if(stateCode =='1'){
					var params = data['data'];
					var totalCounts = params['totalCounts'];
					if(totalCounts > 0){
						layer.msg("查询成功",{offset: '350px'});
						var medicalRecords = params['queryDatas'];
						addRows(medicalRecords);
						var pageSize = params['pageSize'];
						var currentPage = params['currentPage'];
						initPage(parseInt(totalCounts),parseInt(pageSize),parseInt(currentPage));
					}else{
						layer.msg("查询结果为空",{offset: '350px'});
					}
				}else{
					var stateMessage = data['stateMessage'];
					layer.msg(stateMessage,{offset: '350px'});
				}
				
				
			}else{
				var stateMessage = data['stateMessage'];
				layer.msg(stateMessage,{offset: '350px'});
			}
			
		},
		error:function(XMLHttpRequest, textStatus, errorThrown){
			if(XMLHttpRequest.status == 200){
				if(textStatus == 'parsererror'){
					layer.msg("登陆已过期，请重新登陆");
				}else{
					layer.msg("未知错误，请联系管理员");
				}
			}else if(XMLHttpRequest.status == 0){
				if(textStatus == 'error'){
					layer.msg("无法连接服务器，请检测网络");
				}else{
					layer.msg("未知错误，请联系管理员");
				}
			}else{
				layer.msg("未知错误，请联系管理员");
			}
		},
		complete:function(XMLHttpRequest, textStatus){
			layer.closeAll('loading');
			parent.document.getElementById("mainContent").height=document.body.scrollHeight + 50;
			$("input[name='onlyId']").focus();
		}
	});
	
};

/*添加列表行*/
function addRows(medicalRecords){
	for(var index in medicalRecords){
		var medicalRecord = medicalRecords[index];
		var add_content=$("#template_tr").clone();
		add_content.removeAttr("hidden").removeAttr("id");
		add_content.attr("only_id",medicalRecord.ONLYID);
		add_content.attr("mr_id",medicalRecord.MRID);
		add_content.attr("visit_number",medicalRecord.VISITNUMBER);
		add_content.find("span[class='only_id']").html(medicalRecord.ONLYID);
		add_content.find("span[class='mr_id']").html(medicalRecord.MRID);
		add_content.find("span[class='patient_name']").html(medicalRecord.PATIENTNAME);
		add_content.find("span[class='id_number']").html(medicalRecord.IDNUMBER);
		add_content.find("span[class='visit_number']").html(medicalRecord.VISITNUMBER);
		add_content.find("span[class='out_dept_name']").html(medicalRecord.OUTDEPTNAME);
		add_content.find("span[class='out_hospital_date']").html(medicalRecord.OUTHOSPITALDATETIME.substring(0,10));
		add_content.find("span[class='out_hospital_type_name']").html(medicalRecord.OUTHOSPITALTYPENAME);
		/*if(medicalRecord.isExsit && medicalRecord.isExsit =='1'){
			add_content.find("a[class='operate_pigeonhole']").parent().html("已提取");
		}*/
		if(medicalRecord.isExsit && medicalRecord.isExsit =='1'){
			add_content.find("td[data-type='operate']").html("已提取");
		}else{
			add_content.find("td[data-type='operate']").html("" +
					"<a class='operate_pigeonhole' onclick='extractMedicalRecordSubmit(this)' title='提取'>提取&nbsp;</a>");
		}
		
		
		$("#query_show_table tbody").append(add_content);
    } 
	
}

/*清空列表和分页控件*/
function clearPage(){
	$("#template_tr").siblings("tr").remove();
	$("#page_plus").html("");
	$("#totalPage").html("0");
    $("#currentPage").html("0/0");
}

/*初始化分页控件*/
function initPage(totalCounts,pageSize,currentPage){
	 var visiblePages = 5;
	 var totalPages =  totalCounts%pageSize==0?(totalCounts/pageSize):(parseInt(totalCounts/pageSize)+1);
	 if(totalCounts < 1){
		$("#page_plus").html("");
		$("#totalPage").html("0");
        $("#currentPage").html("0/0");
	 }else{
		 $('#page_plus').jqPaginator({

	        totalCounts: totalCounts,
	        pageSize:pageSize,
	        visiblePages: visiblePages,
	        currentPage: currentPage,

	        first:'<li class="first"><a href="javascript:void(0);">首页</a></li>',
	        prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
	        next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
	        last: '<li class="last"><a href="javascript:void(0);">末页</a></li>',
	        page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
	        onPageChange: function (num,type) {

	     		if(type !='init'){
	     			$("input[name='currentPage']").val(num);
	     			queryFormSubmit();
				}
	            $("#totalPage").html(totalCounts);
	            $("#currentPage").html(num+"/"+totalPages);
	        }
	    });
	 }
}

function extractMedicalRecordSubmit(obj){
	
	var basePath = $("#basePath").val();
	var content = $(obj).parent().parent();
	var onlyId = content.attr("only_id");
	var mrId = content.attr("mr_id");
	var visitNumber = content.attr("visit_number");
	
	var submitData="mrId="+mrId+ 
					"&visitNumber="+visitNumber+
					"&onlyId="+onlyId;
	layer.load(1);
	$.ajax({
		url: basePath + "interface_medical_record1/ajax_extract_medical_record_qf_dr_pt",
		type: "POST",
		dataType: "json",
		data:submitData,
		success: function( data ) {
			var success = data['success'];
			if(success){
				var stateCode = data['stateCode'];
				if("1" == stateCode){
					layer.msg("提取成功！",{offset: '350px'});
					var medicalRecord = data['data'];
					$("tr[mr_id='"+medicalRecord.mrId+"'][visit_number='"+medicalRecord.visitNumber+"'] td").last().html("已提取");
				}
				else if("2" == stateCode){
					layer.msg("已提取过，请勿重复提取！",{offset: '350px'});
				}
				else{
					var stateMessage = data['stateMessage'];
					if(stateMessage != null){
						layer.msg(stateMessage,{offset: '350px'});
					}else{
						layer.msg("操作错误，请重试！",{offset: '350px'});
					}
				}
			}else{
				layer.msg("操作错误，请重试！",{offset: '350px'});
			}
			console.log(data);
			
		},
		error:function(XMLHttpRequest, textStatus, errorThrown){
			if(XMLHttpRequest.status == 200){
				if(textStatus == 'parsererror'){
					layer.msg("登陆已过期，请重新登陆");
				}else{
					layer.msg("未知错误，请联系管理员");
				}
			}else if(XMLHttpRequest.status == 0){
				if(textStatus == 'error'){
					layer.msg("无法连接服务器，请检测网络");
				}else{
					layer.msg("未知错误，请联系管理员");
				}
			}else{
				layer.msg("未知错误，请联系管理员");
			}
		},
		complete:function(XMLHttpRequest, textStatus){
			layer.closeAll('loading');
			$("input[name='onlyId']").focus();
		}
	});
}



